/*搜索数据字段转换
*  api接口 http://192.168.0.51:8090/pages/viewpage.action?pageId=17728051
*/
/*四舍五入保留两位有效数字*/
function tofixedFn(num) {
    return Number(num).toFixed(2)
}

function gettconverToProductBean(result) {
    var data = [];
    if (!result != null) {
        var spus = result.data.list;
        if (spus != null && spus.length > 0) {
            for (let i = 0; i < spus.length; i++) {
                const spu = spus[i];
                var state;
                if (spu.spu_sale_status == 0) {
                    state = '未上架';
                } else if (spu.spu_sale_status == 1) {
                    state = '上架';
                } else {
                    state = '下架';
                }
                var minCostPrice = spu.min_cost_price == null ? "" : tofixedFn(spu.min_cost_price);//成本价
                var maxCostPrice = spu.max_cost_price == null ? "" : tofixedFn(spu.max_cost_price);
                var minPrice = spu.min_price == null ? "" : tofixedFn(spu.min_price);//现金价
                var maxPrice = spu.max_price == null ? "" : tofixedFn(spu.max_price);
                var min_retail_price = spu.min_retail_price == null ? "" : tofixedFn(spu.min_retail_price);//售价
                var max_retail_price = spu.max_retail_price == null ? "" : tofixedFn(spu.max_retail_price);//售价
                data.push({
                    _activityId: spu.spuOnActivity,
                    _spuCode: spu.spu_code,
                    _spuName: spu.name,
                    _brand: spu.brand_name,
                    // _area: spu.sale_area,
                    _shelveNum: String(spu.onSale).replace(",", "/"),
                    _costPrice: minCostPrice + ' ~ ' + maxCostPrice,//成本价
                    _tradePrice: minPrice + ' ~ ' + maxPrice,//现金价
                    _Prices: min_retail_price + ' ~ ' + max_retail_price,//售价
                    _state: state,
                    _skus: searchConvertSkus(spu.spu_code, spu.name, spu.childSku)
                })
            }
        }
    }
    return data;
}

// 查询的数据转换sku集合
function searchConvertSkus(spuCode, spuName, skus) {
    var json = [];
    if (skus != null && skus.length > 0) {
        for (let i = 0; i < skus.length; i++) {
            const sku = skus[i];
            var state;
            if (sku.sales_status == 0) {
                state = '未上架';
            } else if (sku.sales_status == 1) {
                state = '上架';
            } else {
                state = '下架';
            }
            if($("#activityPerson").val()==30){
                json.push({
                    _id: 0,
                    _activityId: sku.onActivityId,
                    _skuCode: sku.sku_code,
                    _spuCode: spuCode,
                    _spuName: spuName,
                    _spuCode_productCode: sku.sku_code,
                    _standard: sku.specifications,
                    _unit: String(sku.unit_name),
                    _tradePrice: sku.pifa_price == null ? "" : tofixedFn(sku.pifa_price),//现金价
                    _Prices: tofixedFn(sku.retail_price),//售价
                    _saleprices: tofixedFn(sku.sale_price),
                    _ruleType: 10,
                    _ruleValue: 0,
                    _activity_price: 0.0,
                    _helpCode: sku.helpcode,
                    _limit_num: "",
                    _costPrice: sku.cost_price == null ? "" : stofixedFn(ku.cost_price),//成本价
                    _stock: sku.actual_stock,
                    _specifications: sku.specifications,
                    _salesStatus: state
                });
            }else {
                json.push({
                    _id: 0,
                    _activityId: sku.onActivityId,
                    _skuCode: sku.sku_code,
                    _spuCode: spuCode,
                    _spuName: spuName,
                    _spuCode_productCode: sku.sku_code,
                    _standard: sku.specifications,
                    _unit: String(sku.unit_name),
                    _tradePrice: sku.pifa_price == null ? "" : tofixedFn(sku.pifa_price),//现金价
                    _Prices: tofixedFn(sku.retail_price),//售价
                    _saleprices: tofixedFn(sku.sale_price),
                    _ruleType: 10,
                    _ruleValue: 0,
                    _activity_price: 0.0,
                    _helpCode: sku.helpcode,
                    _limit_num: "",
                    _costPrice: sku.cost_price == null ? "" : tofixedFn(sku.cost_price),//成本价
                    _stock: sku.actual_stock,
                    _specifications: sku.specifications,
                    _salesStatus: state
                });
            }
        }
    }
    return json;
}

// 修改时skulist数据转换
function editConvertSkus(skuList) {
    var skus = [];
    if (skuList.length > 0) {
        for (let i = 0; i < skuList.length; i++) {
            const SKU = skuList[i];
            var activity_price = 0;
            if (SKU.ruleType == 10) {
                activity_price = Math.round((SKU.tradePrice * (SKU.ruleValue / 10)) * 100) / 100;
            } else {
                activity_price = Math.round( (SKU.tradePrice - SKU.ruleValue) * 100) / 100;
            }
            skus.push({
                _id: SKU.id,
                _activityId: SKU.activityId,
                _skuCode: SKU.skuCode,
                _spuCode: SKU.spuCode,
                _spuName: SKU.spuName,
                _spuCode_productCode: SKU.skuCode,
                _standard: SKU.formName,
                _unit: SKU.unitOfMeasurement,
                _tradePrice: tofixedFn(SKU.realTradePrice),//现金价
                _Prices: tofixedFn(SKU.retailPrice),//售价
                _saleprices:tofixedFn( SKU.tradePrice),
                _ruleType: SKU.ruleType,
                _ruleValue: SKU.ruleValue,
                _activity_price: tofixedFn(activity_price),
                _helpCode: SKU.helpCode,
                _limit_num: SKU.limitNum,
                _costPrice: tofixedFn(SKU.costPrice),//成本价
                _stock: SKU.actualStock,
                _specifications: SKU.formName
                /* _salesStatus: SKU.state*/
            });
        }
    }
    return skus;
}

// excel 单品促销 数据格式转换
function converExcelToSkus(data) {
    var json = [];
    if (data.length > 0) {
        for (let i = 0; i < data.length; i++) {
            const SKU = data[i];
            console.log("111111111111111")
            json.push({
                _id: 0,
                _activityId: 0,
                _skuCode: SKU.skuCode,
                _spuCode: SKU.spuCode,
                _spuName: SKU.spuName,
                _spuCode_productCode: SKU.skuCode,
                _standard: SKU.formName,
                _unit: SKU.unitOfMeasurement,
                _tradePrice: tofixedFn(SKU.realTradePrice),//现金价
                _Prices: tofixedFn(SKU.tradePrice),//售价
                _saleprices: tofixedFn(SKU.tradePrice),
                _ruleType: SKU.ruleType == null ? 0 : SKU.ruleType,
                _ruleValue: SKU.ruleValue == null ? 0 : SKU.ruleValue,
                _activity_price: SKU.promotionalPrice == null ? 0 : tofixedFn(SKU.promotionalPrice),
                _helpCode: SKU.helpCode,
                _limit_num: SKU.limitNum == null ? "" : SKU.limitNum,
                _costPrice: tofixedFn(SKU.costPrice),//成本价
                _stock: SKU.actualStock,
                _specifications: SKU.formName,
                _spuSkuActivity:SKU.spuSkuActivity
                /* _salesStatus: SKU.state*/
            });
        }
    }
    return json;
}

/*单品促销保存json转换 数据拼接*/
function convertSkuListToSingle(val) {
    console.log(val)
    var json = {};
    json.activityBase = converForm(val);
    json.activityProductList = convertSkuList();
    return json;
}

/*满减促销保存json转换 数据拼接*/
function convertSkuListToReduce(val) {
    var json = {};
    json.activityBase = converForm(val);
    json.activityFullReductionList = converReduce();
    json.activityProductList = convertSkuList();
    return json;
}

// 单品促销拼接表头
function converForm(val) {
    var form = {};
    form.activityId = this_activityId,
        form.activityName = $("#activityName").val();
    form.startTime = $("#startTime").val();
    form.endTime = $("#endTime").val();
    form.activityPerson = $("#activityPerson").val();
    form.activityLocation = $("#activityLocation").val();
    if ($('#platform-10').is(':checked')) {
        form.platform = 10;
    } else if ($('#platform-20').is(':checked')) {
        form.platform = 20;
    } else {
        form.platform = 30;
    }
    var payway = [];
    $('input[name="payWay"]:checked').each(function () {
        var val = $(this).val();
        if (payway.indexOf(val) <= -1) {
            payway.push(val);
        }
    });
    var payways = '';
    for (let i = 0; i < payway.length; i++) {
        payways += payway[i] + ',';
    }
    form.payWay = payways;
    form.enableCategoryDiscount = $("#enableCategoryDiscount").val();
    form.categoryDiscountRule = $("#categoryDiscountRule").val();
    form.categoryDiscount = $("#categoryDiscount").val();
    form.activityType = activityType;
    form.advertisingWords = $("#advertisingWords").val();
    form.adPcUrl = $("#adPcUrl").val();
    form.adAppUrl = $("#adAppUrl").val();

    var quzhi = JSON.parse(web.readCookie("loginData"));
    form.createUserid = (quzhi.user.user_ID);
    form.createUsername = (quzhi.user.username);
    form.modifyUserid = (quzhi.user.user_ID);
    form.modifyUsername = (quzhi.user.username);
    var activityPerson = "";
    console.log(val)
    if(val){
        for (var i = 0; i < val.length; i++) {
            activityPerson += val[i]+ ",";
        }
        //去掉最后一个逗号(如果不需要去掉，就不用写)
        if (activityPerson.length > 0) {
            activityPerson = activityPerson.substr(0, activityPerson.length - 1);
        }
    }
    form.activityPersons=activityPerson

    return form;
}


//拼接商品数据
function convertSkuList() {
    var SKUS = [];
    if (setSkuLists.length > 0) {
        for (let i = 0; i < setSkuLists.length; i++) {
            const SKU = setSkuLists[i];
            console.log(SKU);
            SKUS.push({
                "id": SKU._id,
                "spuCode": SKU._spuCode,
                "spuName": SKU._spuName,
                "skuCode": SKU._skuCode,
                "skuName": SKU._spuName,
                "helpCode": SKU._helpCode,
                "ruleType": SKU._ruleType,
                "ruleValue": SKU._ruleValue,
                "limitNum": SKU._limit_num,
                "activityProductStatus": 10,
                "productCode": SKU._skuCode,
                "promotionalPrice":SKU._activity_price
            });
        }
    }
    return SKUS;
}

// 拼接满减数据
function converReduce() {
    var reduces = [];
    var fullReductionWay = $("#fullReductionWay").val();
    var fixGrade = $('input[name=fixGrade]:checked').val();
    if (fixGrade == 10) {
        var ruleType = $('input[name=ruleType]:checked').val();
        var ruleValue = $("#ruleValue-" + ruleType).val();
        reduces.push({
            "fullReductionWay": fullReductionWay,
            "fixGrade": fixGrade,
            "gradeLevel": "",
            "useCondition": $("#useCondition-" + fullReductionWay).val(),
            "ruleType": ruleType,
            "ruleValue": ruleValue,
            "preferentialMost": $("#preferentialMost").val()
        });
    } else {  // 层级优惠
        for (let i = 0; i < rowNum; i++) {
            var index = i + 1;
            var ruleType = $('input[name=promotion-' + index + ']:checked').val();
            var ruleValue = ruleType == 10 ? $("#roleValue-count-" + index).val() : $("#roleValue-reduce-" + index).val();
            reduces.push({
                "fullReductionWay": fullReductionWay,
                "fixGrade": fixGrade,
                "gradeLevel": index,
                "useCondition": $("#useCondition-" + fullReductionWay + "-" + index).val(),
                "ruleType": ruleType,
                "ruleValue": ruleValue,
                "preferentialMost": 0
            });
        }
    }
    return reduces;
}
